package com.ghcnb.jyt.cmn.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.ghcnb.jyt.cmn.listener.DictListener;
import com.ghcnb.jyt.cmn.mapper.xml.DictMapper;
import com.ghcnb.jyt.cmn.service.DictService;
import com.ghcnb.jyt.model.cmn.Dict;
import com.ghcnb.jyt.model.hosp.HospitalSet;
import com.ghcnb.jyt.vo.cmn.DictEeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {
    @Override
    public List<Dict> findByParentId(Long parentId) {
        QueryWrapper<Dict> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("parent_id",parentId);
        List<Dict> dictList = baseMapper.selectList(queryWrapper);
        //向list集合每个dict对象中设置hasChildren
        for (Dict dict:dictList){
            Long dictId=dict.getId();
            boolean isChild=this.hasChild(dictId);
            dict.setHasChildren(isChild);
        }
        return dictList;
    }

    //导出数字字典
    @Override
    public void exportDictData(HttpServletResponse response){
        //设置下载信息
        response.setContentType("application/Vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //URLEncoder.encode可以防止中文乱码
        String fileName= null;
        try {
            fileName = URLEncoder.encode("数据字典","UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setHeader("Content-disposition","attachment="+fileName+".xlsx");
        //查询数据库
        List<Dict> dictList = baseMapper.selectList(null);
        //Dict----》dictVo
        List<DictEeVo> dictVoList=new ArrayList<>();
        for(Dict dict:dictList)
        {
            DictEeVo dictEeVo=new DictEeVo();
            BeanUtils.copyProperties(dict,dictEeVo);
            dictVoList.add(dictEeVo);
        }
        //调用方法进行写操作
        try {
            EasyExcel.write(response.getOutputStream(), DictEeVo.class).sheet("dict")
                    .doWrite(dictVoList);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    private DictMapper dictMapper;
    @Override
    public void importDictData(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(),DictEeVo.class,new DictListener(dictMapper)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //判断id下是否有子节点
    private boolean hasChild(Long id){
        QueryWrapper<Dict> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("parent_id",id);
        Integer counnt = baseMapper.selectCount(queryWrapper);
        return counnt>0;
    }

}
